load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

# The Optgen language compiler uses itself to generate its AST expressions.
# This is a form of compiler bootstrapping[1], and needs some manual overrides
# to fit into how Bazel wants things to be. We define a second go_library
# target[2], that sources pre-generated og.go files that are already checked
# into the source tree. These files are used to compile langgen, which is then
# used to generate newer revisions of those same generated files. All
# dependents of [3] will need to depend on the og.go files generated in the
# Bazel sandbox, which is achieved by [4]. Conversely, to instruct
# gazelle/Bazel to resolve langgen's import of [3] appropriately, we add the
# resolve directive below.
#
# [1]: https://en.wikipedia.org/wiki/Bootstrapping_(compilers)
# [2]: //pkg/sql/opt/optgen/lang:bootstrap
# [3]: github.com/cockroachdb/cockroach/pkg/sql/opt/optgen/lang
# [4]: See the "gazelle:resolve" directive in the top-level BUILD.bazel file
#
# gazelle:resolve go github.com/cockroachdb/cockroach/pkg/sql/opt/optgen/lang //pkg/sql/opt/optgen/lang/langbootstrap

go_library(
    name = "langgen_lib",
    srcs = [
        "exprs_gen.go",
        "main.go",
        "ops_gen.go",
    ],
    importpath = "github.com/cockroachdb/cockroach/pkg/sql/opt/optgen/cmd/langgen",
    visibility = ["//visibility:private"],
    deps = [
        "//pkg/sql/opt/optgen/lang/langbootstrap",  # keep
        "@com_github_cockroachdb_errors//:errors",
    ],
)

go_binary(
    name = "langgen",
    embed = [":langgen_lib"],
    visibility = ["//visibility:public"],
)
